package com.fuelstations.coderone;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Locale;

import com.example.androidhive.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.Typeface;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.ShapeDrawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class StationAdapter extends BaseAdapter {

	private Activity activity;
	private ArrayList<HashMap<String, String>> data;
	private static LayoutInflater inflater = null;
	public ImageLoader imageLoader;

	public StationAdapter(Activity a, ArrayList<HashMap<String, String>> d) {
		activity = a;
		data = d;
		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		imageLoader = new ImageLoader(activity.getApplicationContext());
	}

	public int getCount() {
		return data.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)

			vi = inflater.inflate(R.layout.list_row, null);
		//

		TextView unld95sh = (TextView) vi.findViewById(R.id.unld95sh);
		TextView unld98sh = (TextView) vi.findViewById(R.id.unld98sh);
		TextView unld100sh = (TextView) vi.findViewById(R.id.unld100sh);
		TextView diesel_sh = (TextView) vi.findViewById(R.id.diesel_sh);
		TextView dieselheat_sh = (TextView) vi.findViewById(R.id.dieselheat_sh);
		TextView kerosene_sh = (TextView) vi.findViewById(R.id.kerosene_sh);
		//
		View l1 = (View) vi.findViewById(R.id.l1);
		View l2 = (View) vi.findViewById(R.id.l2);
		View l3 = (View) vi.findViewById(R.id.l3);
		View l4 = (View) vi.findViewById(R.id.l4);
		View l5 = (View) vi.findViewById(R.id.l5);
		//
		TextView owner = (TextView) vi.findViewById(R.id.owner);
		TextView address = (TextView) vi.findViewById(R.id.address);
		TextView unld95 = (TextView) vi.findViewById(R.id.unld95);
		TextView unld98 = (TextView) vi.findViewById(R.id.unld98);
		TextView unld100 = (TextView) vi.findViewById(R.id.unld100);
		TextView diesel = (TextView) vi.findViewById(R.id.diesel);
		TextView dieselheat = (TextView) vi.findViewById(R.id.dieselheat);
		TextView kerosene = (TextView) vi.findViewById(R.id.kerosene);

		ImageView thumb_image = (ImageView) vi.findViewById(R.id.logo);

		HashMap<String, String> station = new HashMap<String, String>();
		station = data.get(position);

		// Setting all values in listview
		String lang = Locale.getDefault().getLanguage();
		if (lang.equals("el")) {
			owner.setText(station.get(HashTags.KEY_OWNER));
			address.setText(station.get(HashTags.KEY_ADDRESS));
		} else if (lang.equals("en")) {
			owner.setText(station.get(HashTags.KEY_OWNER_EN));
			address.setText(station.get(HashTags.KEY_ADDRESS_EN));
		}

		setText(station.get(HashTags.KEY_UNLD95), unld95, unld95sh, null);
		setText(station.get(HashTags.KEY_UNLD98), unld98, unld98sh, l1);
		setText(station.get(HashTags.KEY_UNLD100), unld100, unld100sh, l2);
		setText(station.get(HashTags.KEY_DIESEL), diesel, diesel_sh, l3);
		setText(station.get(HashTags.KEY_DIESEL_HEAT), dieselheat, dieselheat_sh, l4);
		setText(station.get(HashTags.KEY_KEROSENE), kerosene, kerosene_sh, l5);

		imageLoader.DisplayImage(station.get(HashTags.KEY_COMPANY), thumb_image);
		return vi;
	}

	private static void setText(String s, TextView tv, TextView tvsh, View l) {
		if (s.equals("0.0")) {
			tvsh.setVisibility(View.GONE);
			tv.setVisibility(View.GONE);
			if (l != null)
				l.setVisibility(View.GONE);
		} else {
			tv.setText(s);
		}
	}

}